package org.wanho.rent.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.wanho.rent.entity.Company;
import org.wanho.rent.entity.LeadingPeriod;
import org.wanho.rent.entity.ProductType;
import org.wanho.rent.entity.SysUser;
import org.wanho.rent.util.PageInfo;
import org.wanho.rent.util.ResponseUtil;

/**
 * 贷款周期控制器
 * @author student
 *
 */

@WebServlet(urlPatterns = "*.period")
public class PeriodController extends HttpServlet {

	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		System.out.println(request.getServletPath() + "###");
		String servletPath = request.getServletPath();
		if (servletPath.equals("/addPeriod.period")) {
			addPeriod(request, response);
			System.out.println("-========+/period");
		} else if (servletPath.equals("/selectAllPeriod.period")) {
			selectAllPeriodByPage(request, response);
		} else if (servletPath.equals("/updatePeriod.period")) {
			updatePeriod(request, response);
		} else if (servletPath.equals("/updatePeriodStatus.period")) {
			updatePeriodStatus(request, response);
		}

	}

	/**
	 * 查询所有贷款周期/分页
	 * @param request
	 * @param response
	 */
	private void selectAllPeriodByPage(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		String all = request.getParameter("all");
		// all不是null的时候就是查询所有，否则必须分页查询
		if (all != null) {

			List list = new ArrayList();

			list.add(new LeadingPeriod(1, "7个月", 0));
			list.add(new LeadingPeriod(2, "8个月", 1));
			list.add(new LeadingPeriod(3, "9个月", 1));
			// 返回数据到浏览器
			ResponseUtil.doResponse("成功！", list, true, response);
			return;

		} else {
			String pageNo = request.getParameter("pageNo");
			System.out.println(pageNo + "#");
			int page = Integer.parseInt(pageNo);

			List list = new ArrayList();

			list.add(new LeadingPeriod(1, "14个月", 0));
			list.add(new LeadingPeriod(2, "8个月", 1));
			list.add(new LeadingPeriod(3, "12个月", 1));
			// 返回数据到浏览器

			int pagesize = 2;
			if (page == 2) {
				ResponseUtil.doResponse("成功！",
						new PageInfo<LeadingPeriod>(3, page, 2, list.subList((page - 1) * pagesize, list.size())), true,
						response);

			} else {
				ResponseUtil.doResponse("成功！",
						new PageInfo<LeadingPeriod>(3, page, 2, list.subList((page - 1) * pagesize, pagesize * page)),
						true, response);
			}

		}

	}

	/**
	 * 修改状态，禁用，启用
	 * @param request
	 * @param response
	 */
	private void updatePeriodStatus(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub

		String periodId = request.getParameter("periodId");
		String status = request.getParameter("status");

		System.out.println("periodId=" + periodId + " status= " + status);

		// 返回数据到浏览器
		ResponseUtil.doResponse("成功！", null, true, response);
	}

	/**
	 * 修改贷款周期
	 * @param request
	 * @param response
	 */
	private void updatePeriod(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		String periodId = request.getParameter("periodId");
		String periodName = request.getParameter("periodName");

		System.out.println("periodId=" + periodId + "## periodName=" + periodName);

		// 返回数据到浏览器
		ResponseUtil.doResponse("成功！", null, true, response);
	}

	/**
	 * 添加贷款周期
	 * @param request
	 * @param response
	 */
	private void addPeriod(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub

		String period = request.getParameter("period");
		System.out.println("period=" + period);

		// 返回数据到浏览器
		ResponseUtil.doResponse("成功！", new SysUser("123", "344"), true, response);
	}

}
